package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.Region;
import com.graduation.smartparkingplatform.Pojo.Space;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/region")
public class RegionController {

  @Autowired RegionService regionService;

  // 获取区域列表
  @GetMapping("/regionList")
  public Response getRegionList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<Region> regionList = regionService.getRegionList(page, count);
      return ResponseUtils.buildSuccess(regionList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 获取该区域下的停车位
  @GetMapping("/spaceByRegion")
  public Response spaceByRegion(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "regionId") Integer regionId) {
    try {
      PageInfo<Space> spaceByRegion = regionService.spaceByRegion(page, count, regionId);
      return ResponseUtils.buildSuccess(spaceByRegion);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchRegion")
  public Response searchRegion(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "regionName", defaultValue = "null") String regionName,
      @RequestParam(value = "regionType", defaultValue = "null") String regionType) {
    try {
      PageInfo<Region> searchList = regionService.searchRegion(page, count, regionName, regionType);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 添加停车场区域
  @PostMapping("/addRegion")
  public Response addRegion(@RequestBody Region region) {
    Boolean addRegion = regionService.addRegion(region);
    if (addRegion) {
      return ResponseUtils.buildSuccess("增加停车场区域成功");
    }
    return ResponseUtils.buildError("增加停车场区域失败");
  }

  // 修改停车场区域
  @PostMapping("/changeInfo")
  public Response changeInfo(@RequestBody Region region) {
    Integer regSpaceCount = region.getReg_space_count();
    Integer regSpaceRecount = region.getReg_space_recount();
    if (regSpaceCount < regSpaceRecount) {
      return ResponseUtils.buildPARAM_ERROR();
    }
    Boolean changeInfo = regionService.changeInfo(region);
    if (changeInfo) {
      return ResponseUtils.buildSuccess("修改停车场区域信息成功");
    }
    return ResponseUtils.buildError("修改停车场区域信息失败");
  }

  // 删除停车场区域
  @PostMapping("/deleteRegion")
  public Response deleteRegion(@RequestParam("regionId") Integer regionId) {
    Boolean delete = regionService.deleteRegion(regionId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除停车场区域成功");
    }
    return ResponseUtils.buildError("删除停车场区域失败");
  }
}
